
angular.module('app.services', ['app.config'],function($httpProvider){
    $httpProvider.interceptors.push(['$q', '$location','$window','$localStorage','ConfigModule','$injector', function($q, $location,$window,$localStorage,ConfigModule,$injector) {
        return {
            'request': function (config) {
                if(config.headers['Content-Type'] == 'application/json;charset=utf-8'){
                    config.headers['Content-Type'] = 'application/x-www-form-urlencoded;charset=utf-8';
                }
                if(config.method == 'POST'){
                    if(config.url.indexOf(ConfigModule.ApiDomain) == -1 || config.url.indexOf(ConfigModule.AccessTokenDomain) == -1){
                        if(!config.access_token){
                            config.url = ConfigModule.ApiDomain+config.url;
                        }else{
                            config.url = ConfigModule.AccessTokenDomain+config.url;
                        }
                    }
                }
                //var access
                //$httpProvider.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded;charset=utf-8';
                config.data = config.data || {};
                config.data.access_token = ($localStorage && $localStorage.access_token)?$localStorage.access_token:'';

                config.headers = config.headers || {};
                return config;
            },
            'response': function(response) {
                var $http = $injector.get('$http');
                if(response.data.status && (response.data.status == 40002)){
                    if(response.config.url != ConfigModule.getToken && response.config && response.config.method == 'POST'){
                        return $http(response.config);
                    }
                }else{
                    return response;
                }

                return response;
            }
        };
    }]);
    $httpProvider.defaults.withCredentials = true;
    var param = function(obj) {
        var query = '', name, value, fullSubName, subName, subValue, innerObj, i;
        for(name in obj) {
            value = obj[name];
            if(value instanceof Array) {
                for(i=0; i<value.length; ++i) {
                    subValue = value[i];
                    fullSubName = name + '[' + i + ']';
                    innerObj = {};
                    innerObj[fullSubName] = subValue;
                    query += param(innerObj) + '&';
                }
            }
            else if(value instanceof Object) {
                for(subName in value) {
                    subValue = value[subName];
                    fullSubName = name + '[' + subName + ']';
                    innerObj = {};
                    innerObj[fullSubName] = subValue;
                    query += param(innerObj) + '&';
                }
            }
            else if(value !== undefined && value !== null)
                query += encodeURIComponent(name) + '=' + encodeURIComponent(value) + '&';
        }
        return query.length ? query.substr(0, query.length - 1) : query;
    };
    $httpProvider.defaults.transformRequest = [function(data) {
        return angular.isObject(data) && String(data) !== '[object File]' ? param(data) : data;
    }];
})